package com.ruoyi.sycs.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.sycs.listener.BaQjDetailVoImportListener;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckLogin;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.sycs.domain.vo.BaQjDetailVo;
import com.ruoyi.sycs.domain.bo.BaQjDetailBo;
import com.ruoyi.sycs.service.IBaQjDetailService;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 器具明细表
 *
 * @author zyb
 * @date 2024-04-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/sycs/baQjDetail")
public class BaQjDetailController extends BaseController {

    private final IBaQjDetailService iBaQjDetailService;

    /**
     * 查询器具明细表列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public TableDataInfo<BaQjDetailVo> list(BaQjDetailBo bo, PageQuery pageQuery) {
        return iBaQjDetailService.queryPageList(bo, pageQuery);
    }

    /**
     * 器具明细表分页
     */
    @SaCheckLogin
    @GetMapping("/page")
    public TableDataInfo<BaQjDetailVo> page(BaQjDetailBo bo, PageQuery pageQuery) {
        return iBaQjDetailService.page(bo, pageQuery);
    }

    /**
     * 某个excel 某个日期中 可用的 器具明细表分页
     */
    @SaCheckLogin
    @GetMapping("/usablePage")
    public TableDataInfo<BaQjDetailVo> usablePage(@Validated(QueryGroup.class) BaQjDetailBo bo, PageQuery pageQuery) {
        return iBaQjDetailService.usablePage(bo, pageQuery);
    }

    /**
     * 导出器具明细表列表
     */
    @SaCheckLogin
    @Log(title = "器具明细表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BaQjDetailBo bo, HttpServletResponse response) {
        List<BaQjDetailVo> list = iBaQjDetailService.queryList(bo);
        ExcelUtil.exportExcel(list, "器具明细表", BaQjDetailVo.class, response);
    }

    /**
     * 获取器具明细表详细信息
     *
     * @param detailId 主键
     */
    @SaCheckLogin
    @GetMapping("/{detailId}")
    public R<BaQjDetailVo> getInfo(@NotNull(message = "主键不能为空")
                                   @PathVariable Long detailId) {
        return R.ok(iBaQjDetailService.queryById(detailId));
    }

    /**
     * 新增器具明细表
     */
    @SaCheckLogin
    @Log(title = "器具明细表", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BaQjDetailBo bo) {
        return toAjax(iBaQjDetailService.insertByBo(bo));
    }

    /**
     * 修改器具明细表
     */
    @SaCheckLogin
    @Log(title = "器具明细表", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BaQjDetailBo bo) {
        return toAjax(iBaQjDetailService.updateByBo(bo));
    }

    /**
     * 删除器具明细表
     *
     * @param detailIds 主键串
     */
    @SaCheckLogin
    @Log(title = "器具明细表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{detailIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] detailIds) {
        return toAjax(iBaQjDetailService.deleteWithValidByIds(Arrays.asList(detailIds), true));
    }

    /**
     * 获取导入模板
     */
    @SaCheckLogin
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "盒(筒)数据", BaQjDetailVo.class, response);
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     * @param deptId        deptId
     * @param qjId          qjId
     */
    @Log(title = "器具明细表", businessType = BusinessType.IMPORT)
    @SaCheckLogin
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @DSTransactional
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport, @NotNull(message = "deptId is not allow null") Long deptId, @NotNull(message = "qjId is not allow null") Long qjId) throws Exception {
        ExcelResult<BaQjDetailVo> result = ExcelUtil.importExcel(file.getInputStream(), BaQjDetailVo.class, new BaQjDetailVoImportListener(updateSupport, deptId, qjId));
        return R.ok(result.getAnalysis());
    }
}
